package com.magus.project.yddemo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.core.util.lang.StringUtils;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.framework.system.entity.Company;
import com.magus.framework.system.service.CompanyService;
import com.magus.framework.system.util.SecurityUtils;
import com.magus.project.yddemo.entity.IndicatorData;
import com.magus.project.yddemo.service.IndicatorDataService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 环保月填报
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Api(tags = "IndicatorDataPlantMonthSaManager", description = "环保月填报")
@RestController
@RequestMapping("/yddemo/dataPlantMonthSas")
@AppGroupInfo(key = "yddemo:dataPlantMonthSa", name = "环保月填报", appKey = "yddemo")
public class IndicatorDataPlantMonthSaController extends BaseController {

	// @Autowired
	// private IndicatorDataPlantService dataPlantService;

	@Autowired
	private IndicatorDataService indicatorDataService;

	@Autowired
	private CompanyService companyService;

	SimpleDateFormat month = new SimpleDateFormat("yyyy-MM");

	@GetMapping("/findCompanys")
	@RequiresPermissions("yddemo:dataPlantMonthSa:view")
	@ApiOperation(value = "查找公司", notes = "查找公司")
	ResponseEntity<List<Company>> findCompanys() {
		Boolean isSysadmin = SecurityUtils.isSysAdmin();
		List<Company> companies = new ArrayList<>();
		if (isSysadmin) {
			companies = companyService.findAll();
		} else {
			List<String> uniqueKeys = SecurityUtils.findCurrentOrgScope();

			for (String key : uniqueKeys) {
				Company company = companyService.findByUniqueKey(key);
				if (company != null) {
					companies.add(company);
				}
			}
		}

		// String[] result = new String[];

		// 查找当前登陆用户关联的公司
		Company company = null;
		List<String> uniqueKeyList = SecurityUtils.findCompanies();
		if (uniqueKeyList != null && uniqueKeyList.size() > 0) {
			String uniqueKey = uniqueKeyList.get(0);
			company = companyService.findByUniqueKey(uniqueKey);
		}

		List<Company> companyList = new ArrayList<>();

		if (company != null) {
			Company comp = new Company();
			comp.setId(company.getId());
			comp.setName(company.getName());

			companyList.add(comp);
		}
		for (Company comp : companies) {
			if (company != null && comp.getId().equals(company.getId())) {
			} else {
				Company compa = new Company();
				compa.setId(comp.getId());
				compa.setName(comp.getName());

				companyList.add(compa);
			}
		}

		return ResponseEntity.ok(companyList);
	}

	/**
	 * 查询环保月填报
	 * 
	 * @param date
	 *            日期
	 * @param companyId
	 *            电厂
	 * @return 指标数据集合
	 */
	@GetMapping
	@RequiresPermissions("yddemo:dataPlantMonthSa:view")
	@ApiOperation(value = "查询环保月填报", notes = "查询此电厂下面的环保月填报")
	@AppFunctionInfo(key = "yddemo:dataPlantMonthSa:view", name = "查询环保月填报列表", groupKey = "yddemo:dataPlantMonthSa", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/dataPlantMonthSas/page/view")
	ResponseEntity<List<IndicatorData>> findAll(@RequestParam(required = false) String companyId,
			@RequestParam(required = false) String date) {
		// @RequestParam(required = false) String companyId, @CustomPagingParam
		// PagingParam paginParam) {
		Date bDate = new Date();
		try {
			bDate = month.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		List<SearchFilter> searchFilters = new ArrayList<>();
		if (StringUtils.isNotBlank(companyId)) {
			searchFilters.add(SearchFilter.equal("indicatorDetail.company.id", companyId));
		}
		// searchFilters.add(SearchFilter.like("indicatorDetail.company.name",
		// "沙角A"));

		searchFilters.add(SearchFilter.like("indicatorDetail.indicator.major.name", "环保"));
		searchFilters.add(SearchFilter.like("indicatorDetail.indicator.dimension", "month"));
		searchFilters.add(SearchFilter.equal("indicatorDetail.dimension", "month"));
		searchFilters.add(SearchFilter.equal("indicatorDetail.indicator.group.name", "环保指标"));
		searchFilters.add(SearchFilter.equal("time", bDate.getTime()));
		searchFilters.add(SearchFilter.equal("dimension", "month"));

		// paginParam.setSortProperty("time");
		// paginParam.setSortDirection("asc");

		// PageRequest request = buildPageRequest(paginParam);
		// Specification<IndicatorDataPlant> spec =
		// buildFilterRequest(searchFilters);

		// return ResponseEntity.ok(dataPlantService.findAll(spec, request));

		Specification<IndicatorData> spec = buildFilterRequest(searchFilters);
		return ResponseEntity.ok(indicatorDataService.findAll(spec));
	}

}
